#include "Avalon/IOTests/NullClientConnection.hpp"

using namespace Avalon;
using namespace Avalon::IO;
using namespace Avalon::IO::Tests;
using namespace boost;

NullClientConnection::NullClientConnection()
    : m_isOpen(false) {}

NullClientConnection::~NullClientConnection() {
  Close();
}

void NullClientConnection::Open() {
  lock_guard<mutex> lock(m_mutex);
  m_isOpen = true;
}

bool NullClientConnection::IsOpen() const {
  lock_guard<mutex> lock(m_mutex);
  return m_isOpen;
}

void NullClientConnection::Close() {
  shared_ptr<ClosedSlot> closedSlot;
  {
    lock_guard<mutex> lock(m_mutex);
    if(!m_isOpen) {
      return;
    }
    m_isOpen = false;
    if(m_closedSlot == NULL) {
      return;
    }
    closedSlot = m_closedSlot;
  }
  (*closedSlot)();
}

void NullClientConnection::SetClosedSlot(const ClosedSlot& slot,
    Out<bool> isOpen) {
  lock_guard<mutex> lock(m_mutex);
  *isOpen = m_isOpen;
  m_closedSlot.reset(new ClosedSlot(slot));
}
